#include    "setting.h"
#include "EEPROM.h"
LV_FONT_DECLARE(siyuanrouti_20);
extern lv_indev_t * indev_keypad;
extern lv_group_t * g;
//回到funcion界面
void setting_to_funcion(lv_event_t*e){
	lv_obj_t*obj=lv_event_get_target(e);
	lv_group_remove_obj(obj);
	lv_indev_set_group(indev_keypad, g);
	back_to_funcion(obj);
}

lv_obj_t*yuyan_label=NULL;
lv_obj_t*beijing_label=NULL;
lv_obj_t*lianan_label=NULL;
uint8_t yuyan_flag=0;    //默认英文
//语言切换回调函数
static void change_lua(lv_event_t * e){
   lv_obj_t*obj=lv_event_get_target(e);
   //英文
   if(lv_obj_has_state(obj,LV_STATE_CHECKED)){
    lv_label_set_text(yuyan_label,"Chin/Eg");
   lv_label_set_text(beijing_label,"back");
   lv_label_set_text(lianan_label,"D_cli");
   yuyan_flag=1;
   }
   //中文
   else{
   yuyan_flag=0;
   lv_label_set_text(yuyan_label,"中/英");
   lv_label_set_text(beijing_label,"暗色");
   lv_label_set_text(lianan_label,"连按");
   }
    EEPROM.begin(4096);
    EEPROM.write(0, yuyan_flag);
    EEPROM.commit();
}

extern lv_obj_t *meter;
uint8_t back_flag=1;//0 关闭  1开启
//背景切换回调函数
static void change_beijing(lv_event_t * e){
   lv_obj_t*back_switch=lv_event_get_target(e);
   lv_obj_t*obj=(lv_obj_t*)lv_obj_get_user_data(back_switch);
   //开启
   if(lv_obj_has_state(back_switch,LV_STATE_CHECKED)){
    lv_obj_set_style_bg_color(meter,lv_color_hex(0x0),LV_PART_MAIN );//时钟背景颜色
   // lv_obj_set_style_bg_color(obj,lv_color_hex(0x286BB4),NULL );//设置背景颜色
    back_flag=1;
   }
   //关闭
   else{
    lv_obj_set_style_bg_color(meter,lv_color_hex(0xF8D998),LV_PART_MAIN );
    //lv_obj_set_style_bg_color(obj,lv_color_hex(0xA2CE59),NULL );//设置背景颜色
    back_flag=0;
   }
    EEPROM.begin(4096);
    EEPROM.write(1, back_flag);
    EEPROM.commit();
}

extern uint8_t lianan_flag;
//置位连按标志位
void Set_lianan_flag(lv_timer_t * timer){
        lianan_flag=1;
        EEPROM.begin(4096);
        EEPROM.write(2, lianan_flag);
        EEPROM.commit();
}



//连按切换回调函数
static void change_lianan(lv_event_t * e){
    lv_obj_t*lianan_switch=lv_event_get_target(e);
       //开启
   if(lv_obj_has_state(lianan_switch,LV_STATE_CHECKED)){
      lv_timer_t *lianan_timer=lv_timer_create(Set_lianan_flag, 500, NULL);//延时置位连按标志位，防止误触
      lv_timer_set_repeat_count(lianan_timer,1);
   }
   //关闭
   else{
        lianan_flag=0;   
         EEPROM.begin(4096);
          EEPROM.write(2, lianan_flag);
          EEPROM.commit();
   }
}

void Setting_Init(lv_obj_t*obj){
	static lv_style_t style_label;
    static lv_style_t style_switch_fous;
    lv_style_init(&style_label);
	lv_style_set_text_font(&style_label,&siyuanrouti_20);
	lv_style_set_text_color(&style_label,lv_color_hex(0x000000));

    //to initialize one style to decorate the btn when foucus
	lv_style_init(&style_switch_fous);

   if(back_flag) 	 lv_obj_set_style_bg_color(obj,lv_color_hex(0xB8B9BD),NULL );//设置背景颜色
	else       	lv_obj_set_style_bg_color(obj,lv_color_hex(0xF8D998),NULL );//设置背景颜色
   
    //语言切换
    lv_obj_t*yuyan_switch=lv_switch_create(obj);
    yuyan_label=lv_label_create(obj);
    if(yuyan_flag) lv_obj_add_state(yuyan_switch, LV_STATE_CHECKED);//默认开英语模式
     else           lv_obj_clear_state(yuyan_switch, LV_STATE_CHECKED);//默 
    lv_obj_align(yuyan_switch, LV_ALIGN_TOP_MID, 0, 30);
    lv_obj_align(yuyan_label, LV_ALIGN_TOP_MID, -60, 30);
    lv_obj_add_style(yuyan_label,&style_label,NULL);
    lv_obj_add_style(yuyan_switch,&style_switch_fous,LV_STATE_FOCUSED);//选择时候变化
    lv_obj_add_event_cb(yuyan_switch, change_lua, LV_EVENT_VALUE_CHANGED, NULL);
    //设置标签Chin/Eg
    if(yuyan_flag) lv_label_set_text(yuyan_label,"Chin/Eg");
    else          lv_label_set_text(yuyan_label,"中/英");
    
    //暗色模式
    lv_obj_t*back_switch=lv_switch_create(obj);
    if(back_flag) lv_obj_add_state(back_switch, LV_STATE_CHECKED);//默认开暗色模式
    else         lv_obj_clear_state(back_switch, LV_STATE_CHECKED);//默
    lv_obj_align(back_switch,LV_ALIGN_CENTER,0,0);
    lv_obj_add_event_cb(back_switch, change_beijing, LV_EVENT_VALUE_CHANGED , obj);
    beijing_label=lv_label_create(obj);
    lv_obj_add_style(beijing_label,&style_label,NULL);
    lv_obj_add_style(back_switch,&style_switch_fous,LV_STATE_FOCUSED);//选择时候变化
    lv_obj_align(beijing_label, LV_ALIGN_CENTER, -60, 0);
    //设置标签
    if(yuyan_flag) lv_label_set_text(beijing_label,"back");
    else          lv_label_set_text(beijing_label,"暗色");
    //连按模式
    lv_obj_t*lianan_switch=lv_switch_create(obj);
    if(lianan_flag) lv_obj_add_state(lianan_switch, LV_STATE_CHECKED);//默认开连按模式
    else             lv_obj_clear_state(lianan_switch, LV_STATE_CHECKED);//默
    lv_obj_align(lianan_switch,LV_ALIGN_CENTER,0,50);
    lv_obj_add_event_cb(lianan_switch, change_lianan, LV_EVENT_VALUE_CHANGED , NULL);
    lianan_label=lv_label_create(obj);
    lv_obj_add_style(lianan_label,&style_label,NULL);
    lv_obj_add_style(lianan_switch,&style_switch_fous,LV_STATE_FOCUSED);//选择时候变化
    lv_obj_align(lianan_label, LV_ALIGN_CENTER, -60, 50);
    //设置标签
    lv_label_set_text(lianan_label,"D_cli");
    //设置标签
    if(yuyan_flag) lv_label_set_text(lianan_label,"D_cli");
    else          lv_label_set_text(lianan_label,"连按");
    lv_group_add_obj(g,obj);
    lv_indev_set_group(indev_keypad, g);
    lv_obj_add_event_cb(obj,setting_to_funcion,LV_EVENT_CLICKED,NULL);
}